home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / hsrc_117.zip / DOSWAP.C < prev    next >
Text File  |  1990-07-29  |  5KB  |  153 lines

  1. #include "stdlib.h"
  2. #include "alloc.h"
  3. #include "stdio.h"
  4. #include "conio.h"
  5. #include "dos.h"
  6. #include "string.h"
  7. #include "mem.h"
  8. #include "io.h"
  9.  
  10. char   SwapName[66];
  11. extern char   swapname[];
  12. extern char   useswapdisk;
  13. extern char   LIMEMS;
  14. unsigned long BytesSwapped;
  15. unsigned int  PrefixSeg;
  16. char           FileAllocated=0;
  17. unsigned int  EmsHandle;
  18. char           EmsAllocated;
  19. unsigned int  FrameSeg;
  20. unsigned int  FileHandle;
  21. unsigned       EmsPageSize=16384;
  22.  
  23. #define FALSE 0
  24.  
  25. extern unsigned cdecl _psp;
  26.  
  27.  
  28. /* NOTE!  If you're using this in a memory model other than any of the
  29. **  Large code size models, you'll need to make these all *far* calls!
  30. */
  31.  
  32. extern char far pascal ALLOCATESWAPFILE(), far pascal EMSINSTALLED();
  33. extern unsigned far pascal EMSPAGEFRAME(), far pascal ALLOCATEEMSPAGES (unsigned),
  34.   far pascal EXECWITHSWAP (char far *, char far *);
  35. extern void far pascal DEALLOCATEEMSHANDLE(unsigned), huge FIRSTTOSAVE(),
  36.   far pascal DEALLOCATESWAPFILE();
  37.  
  38.  
  39. int pascal doswap (char far *command_str, char far *args)
  40.  
  41. /*
  42. **    The first calling parameter *must* include the complete program name
  43. **  and path, including the program name's file extension!  You can easily
  44. **  get this with "searchpath()"    Example:  p = searchpath("PKZIP.EXE");
  45. **  and then pass "p" as the first parameter to "doswap()".
  46. **
  47. **    The second calling parameter above is a string of parameters to pass
  48. **  to the external program, or "" if none.  With the example given above,
  49. **  you might use "-ex arc-name filesone.ext filestwo.exe" to archive both
  50. **  the "filesone.ext" and "filestwo.exe" into ARC-NAME.ZIP using PKZip.
  51. */
  52.  
  53. {
  54.  char exec_tail[128], exec_file[128], far *ivt, huge *end_mem, huge *start_mem,
  55.    far *vptr;
  56.  int swapcode;
  57.  long freefarmem;
  58.  unsigned EMS_pagesneeded;
  59.  extern char **_heaptop;
  60.  
  61.   for (swapcode= 6; swapcode < 20; swapcode++) {
  62.       _close(swapcode);
  63.   }
  64.   sprintf(SwapName, "%c:%s",useswapdisk,swapname);
  65.   vptr = MK_FP(0x0000, 0x0000);
  66.   ivt = farmalloc(1024);
  67.  
  68. /*
  69. THIS IS THE REAL WAY TO DO THE FOLLOWING, BUT UNFORTUNATELY, TURBO C CHEATS
  70. AND CLAIMS *ALL* AVAILABLE RAM AT LOAD TIME, AND DOESN'T HAVE TO BOTHER WITH
  71. ADJUSTING THIS WHEN MEMORY IS DYNAMICALLY ALLOCATED.  THE ONLY TIME IT IS SET
  72. IS WHEN THE "spawnlp()" PROCEDURE SHELLS OUT or "system()" IS USED SO PROGRAMS
  73. HAVE MEMORY AVAILABLE.  Oh, well - so much for "doing it right" <grin>...
  74.         (the unsigned integer at _PSP+2 is supposed to be the
  75.          exact number of paragraphs the program has assigned)
  76.  
  77.  
  78.  unsigned last_seg;
  79.  long memallocated;
  80.  
  81.   last_seg = *((unsigned far *)(MK_FP(_psp, 2)));
  82.   memallocated = 16L * (long)(last_seg - _psp);
  83.  
  84.  
  85. Instead I compute the exact size of memory between the _FirstToSwap label in
  86. the SWAP.ASM code and **__heaptop, the end of available memory, then subtract
  87. the amount of available dynamic memory (far heap) and fudge it by 3k hopefully
  88. getting away with there being less than 3k worth of fragmentation from holes
  89. left in that area by freeing pointers.  If less than 3k of far RAM is free, I
  90. swap the whole thing - right to the top of memory but never beyond.
  91.  
  92. */
  93.  
  94.   start_mem = (char huge *) FIRSTTOSAVE;
  95.   end_mem = (char huge *) _heaptop;
  96.   freefarmem = farcoreleft();
  97.   if (freefarmem < 3072)
  98.     freefarmem = 0;
  99.   else
  100.     freefarmem -= 3072;
  101.   BytesSwapped = (unsigned long) (end_mem - start_mem) - freefarmem;
  102.  
  103. /* ----------------- */
  104.  
  105.   PrefixSeg = _psp;
  106.  
  107.   fputs("\n\x1b[K",stdout);
  108.   if ((LIMEMS) && (EMSINSTALLED ()))
  109.   {
  110.     EMS_pagesneeded = (unsigned) ((BytesSwapped + EmsPageSize + 1) / EmsPageSize);
  111.     if ((EmsHandle = ALLOCATEEMSPAGES (EMS_pagesneeded)) != 0xffff)
  112.     {
  113.       EmsAllocated = 1;
  114.       FrameSeg = EMSPAGEFRAME();
  115.       printf("Swapping %lu bytes to EMS - %u pages (%dk)\n", BytesSwapped, EMS_pagesneeded, 16 * EMS_pagesneeded);
  116.     }
  117.   }
  118.   else
  119.     EmsAllocated = FALSE;
  120.   if (!EmsAllocated && (FileAllocated = ALLOCATESWAPFILE ()) == 0)
  121.   {
  122.     printf("\n\x1b[K");
  123.     printf("ERROR! Cannot Swap to EMS or Disk File `%s'\n", SwapName);
  124.     return(-1);
  125.   }
  126.   if (FileAllocated)
  127.     printf("Swapping %lu bytes to disk file `%s'...\n", BytesSwapped, SwapName);
  128.   strcpy (exec_file, command_str);
  129.   strcpy (exec_tail, " ");
  130.   strcat (exec_tail, args);
  131.   strcat (exec_tail, "\r");
  132.   exec_tail[0] = (char) strlen (exec_tail) - 2;
  133.   disable();
  134.   memcpy((char far *) ivt, (char far *) vptr, 1024);
  135.   enable();
  136.   swapcode = EXECWITHSWAP ((char far *) exec_file, (char far *) exec_tail);
  137.   disable();
  138.   memcpy((char far *) vptr, (char far *) ivt, 1024);
  139.   enable();
  140.   if (EmsAllocated)
  141.   {
  142.     DEALLOCATEEMSHANDLE (EmsHandle);
  143.     EmsAllocated = 0;
  144.   }
  145.   else if (FileAllocated)
  146.   {
  147.     DEALLOCATESWAPFILE ();
  148.     FileAllocated = 0;
  149.   }
  150.   farfree ((char far *) ivt);
  151.   return (swapcode);
  152. }
  153.